/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.anwserai.demos.web.controller;

import com.example.anwserai.demos.web.entity.DocumentAnalysisResult;
import com.example.anwserai.demos.web.service.AIServiceImpl;
import com.example.anwserai.demos.web.util.FileConvertUtils;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Controller
@RequestMapping("/")
public class AnswerController {


    @Autowired
    private AIServiceImpl fileTable4Service;

    // http://127.0.0.1:8080/parse
    @RequestMapping("/parse")
    @ResponseBody
    public Map parseDocument(@RequestParam("file") MultipartFile file) throws IOException {
        // 1. 解析文件并生成结果
        System.out.println("sas:::::"+file.getName());;
        //File newfile = convertMultipartFileToFile(file);
        File newfile = FileConvertUtils.saveMultipartFileToDir(file,"E:\\work\\安装包\\kkfileview\\kkFileView-4.0.0\\file\\demo");
        String fileUrl = newfile.getName();
        System.out.println("nihao::::"+(fileUrl));
        DocumentAnalysisResult result = fileTable4Service.extractMainInfo(newfile);
        result.setFileContent(AIServiceImpl.read(newfile));

        Map map = new HashMap();
        map.put("result",result) ;
        map.put("fileUrl",fileUrl) ;
        System.out.println("error:::::"+result.getErrorcontent());
        // 2. 返回解析结果
        return map;
    }

    public File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {
        // 创建临时文件
        Path tempFile = Files.createTempFile("upload-", ".tmp");
        File file = tempFile.toFile();
        System.out.println(file.getName());

        // 将 MultipartFile 的内容复制到临时文件
        try (InputStream inputStream = multipartFile.getInputStream()) {
            Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
        }

        // 返回 File 对象
        return file;
    }





    // http://127.0.0.1:8080/analysis
    @RequestMapping(value = "/analysis", method = RequestMethod.POST)
    @ResponseBody
    public Map getLogin(@RequestBody Map<String, Object> params) {
        System.err.println(params);
        List dataList=new ArrayList<>();
        Map data=new HashMap<>();
        data.put("id","1");
        data.put("title","郑州市采用AI技术精准检验提升扶贫效果");
        data.put("date","2025年3月10日");
        data.put("source","郑州市政府网站");
        data.put("url","https://www.zhengzhou.gov.cn/news6/8801278.jhtml");
        dataList.add(data);
        Map info=new HashMap<>();
        info.put("msg","111");
        info.put("code",200);
        info.put("data",dataList);
        return info;
    }

    // http://127.0.0.1:8080/javabeat/somewords
    @RequestMapping(value = "/javabeat/{regexp1:[a-z-]+}", method = RequestMethod.GET)
    @ResponseBody
    public String getRegExp(@PathVariable("regexp1") String regexp1) {
        return "URI Part : " + regexp1;
    }
}
